<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class ShopGoodsModel extends AbstractModel
{
    protected $tableName = 't_shop_goods';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws Exception|Throwable
     */
    public function getGoods($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addGoods($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('商城-商品-内容');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('sid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment('商户id');
        $table->colVarChar('code', 20)->setDefaultValue('')->setColumnComment('商品编号');
        $table->colVarChar('name', 500)->setDefaultValue('')->setColumnComment('商品名称');
        $table->colVarChar('marks', 999)->setDefaultValue('')->setColumnComment('商品标签');
        $table->colVarChar('cateids', 999)->setDefaultValue('')->setColumnComment('分类编号');
        $table->colVarChar('s_cateids', 999)->setDefaultValue('')->setColumnComment('商户内部分类编号');
        $table->colVarChar('cover', 999)->setDefaultValue('')->setColumnComment('商品封面');
        $table->colLongText('slider')->setColumnComment('轮播图片');
        $table->colVarChar('remark', 999)->setDefaultValue('')->setColumnComment('商品描述');
        $table->colLongText('content')->setColumnComment('商品详情');
        $table->colVarChar('payment', 999)->setDefaultValue('')->setColumnComment('支付方式');
        $table->colLongText('data_specs')->setColumnComment('商品规格(JSON)');
        $table->colLongText('data_items')->setColumnComment('商品规格(JSON)');
        $table->colBigInt('stock_total', 20)->setDefaultValue(0)->setColumnComment('商品库存统计');
        $table->colBigInt('stock_sales', 20)->setDefaultValue(0)->setColumnComment('商品销售统计');
        $table->colBigInt('stock_virtual', 20)->setDefaultValue(0)->setColumnComment('商品虚拟销量');
        $table->colBigInt('good_comment', 20)->setDefaultValue(0)->setColumnComment('好评数量');
        $table->colDecimal('price_selling', 20, 2)->setDefaultValue(0.00)->setColumnComment('最低销售价格');
        $table->colDecimal('price_market', 20, 2)->setDefaultValue(0.00)->setColumnComment('最低市场价格');
        $table->colBigInt('discount_id', 20)->setDefaultValue(0)->setColumnComment('折扣方案编号');
        $table->colVarChar('truck_code', 20)->setDefaultValue('')->setColumnComment('物流运费模板');
        $table->colTinyInt('truck_type', 1)->setDefaultValue(0)->setColumnComment('物流配送(0无需配送,1需要配送)');
        $table->colTinyInt('rebate_type', 1)->setDefaultValue(1)->setColumnComment('参与返利(0无需返利,1需要返利)');
        $table->colTinyInt('vip_entry', 1)->setDefaultValue(0)->setColumnComment('入会礼包(0非入会礼包,1是入会礼包)');
        $table->colBigInt('vip_upgrade', 20)->setDefaultValue(0)->setColumnComment('购买升级等级(0不升级,其他升级)');
        $table->colBigInt('limit_low_vip', 20)->setDefaultValue(0)->setColumnComment('限制最低等级(0不限制,其他限制)');
        $table->colBigInt('limit_max_num', 20)->setDefaultValue(0)->setColumnComment('最大购买数量(0不限制,其他限制)');
        $table->colBigInt('num_read', 20)->setDefaultValue(0)->setColumnComment('访问阅读统计');
        $table->colTinyInt('state_hot', 1)->setDefaultValue(0)->setColumnComment('设置热度标签');
        $table->colTinyInt('state_home', 1)->setDefaultValue(0)->setColumnComment('设置首页推荐');
        $table->colBigInt('sort', 20)->setDefaultValue(0)->setColumnComment('列表排序权重');
        $table->colTinyInt('status', 1)->setDefaultValue(1)->setColumnComment('商品状态(1使用,0禁用)');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('idx_shop_goods_code', 'code');
        $table->indexNormal('idx_shop_goods_status', 'status');
        $table->indexNormal('idx_shop_goods_deleted', 'is_deleted');
        return $table;
    }
}